package com.yunchen.BooksManagementSystem.manage.servlet;

import com.alibaba.fastjson.JSON;
import com.yunchen.BooksManagementSystem.manage.dao.adminDaoImpl;
import com.yunchen.BooksManagementSystem.manage.entity.borrowingRecord;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet(name = "AgreeReturnServlet",value = "/AgreeReturnServlet")
public class AgreeReturnServlet extends HttpServlet {
    private adminDaoImpl ad = new adminDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        System.out.println("同意归还");
        BufferedReader br=req.getReader();
        // String params= br.readLine();
        StringBuilder builder=new StringBuilder();
        String line=null;
        while ((line=br.readLine())!=null){
            builder.append(line);
        }
        System.out.println(builder);
        borrowingRecord brd=JSON.parseObject(String.valueOf(builder),borrowingRecord.class);
        String userName= brd.getName();
        String bookName= brd.getBookName();
        int count;
        try {
            count=ad.agreeReturnBook(userName,bookName);
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(count);
        //响应成功标识
        resp.setContentType("text/json;charset=UTF-8");
        if (count>0){
            resp.getWriter().write("归还成功");
        }else {
            resp.getWriter().write("归还失败");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
